package org.jetnuke.dao.sqlserver;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Stories;

public class SqlServerStoriesDAO extends org.jetnuke.dao.generic.GenericStoriesDAO {

	
	public Stories getTopByTopic(int topic) throws Exception {
		String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
		String q2 = SystemGlobals.getSql("StoryModel.getTopByTopic");
		
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(q1+" 1 "+q2);
		p.setInt(1,topic);
		ResultSet rs = p.executeQuery();
		
		Stories s = new Stories();
		
		if (rs.next()) {
			fillStoryFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}
	public List selectOrderByCounterDescLimit(int limit) throws Exception {
		String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
		String q2 = SystemGlobals.getSql("StoryModel.selectOrderByCounterDescLimit");
		
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(q1+" "+limit+" "+q2);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	public List selectVotedOrderScoreDesc(int limit) throws Exception {
		String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
		String q2 = SystemGlobals.getSql("StoryModel.selectVotedOrderScoreDesc");
		
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(q1+" "+limit+" "+q2);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	public List selectCommentedOrderCommentDesc(int limit) throws Exception {
		String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
		String q2 = SystemGlobals.getSql("StoryModel.selectCommentedOrderCommentDesc");
		
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(q1+" "+limit+" "+q2);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	
	public List selectOrderBySidDescLimit(int limit) throws Exception {
		String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
		String q2 = SystemGlobals.getSql("StoryModel.selectOrderBySidDescLimit");
		
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(q1+" "+limit+" "+q2);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	public List getStoriesByTopid_SidDesc(int topic, int limit)throws Exception{
		
		PreparedStatement p;
		
		if(topic==0){
			String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
			String q2 = SystemGlobals.getSql("StoryModel.getStoriesSidDesc");
			p = JForumExecutionContext.getConnection().prepareStatement(q1+" "+limit+" "+q2);
		}
		else{
			String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
			String q2 = SystemGlobals.getSql("StoryModel.getStoriesSidDesc1");
			p = JForumExecutionContext.getConnection().prepareStatement(q1+" "+limit+" "+q2);
			p.setInt(1,topic);
		}
		
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	
	public List getStoriesByCategory_SidDesc(int catid, int limit)
			throws Exception {
		String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
		String q2 = SystemGlobals.getSql("StoryModel.getStoriesByCategory_SidDesc");
		
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(q1+" "+limit+" "+q2);
		p.setInt(1,catid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
}
